﻿/*
  作者：bilibili_cly
  版权所有：© 2024 bilibili_cly
  B站主页：https://space.bilibili.com/642010138
  许可：除非授权，否则仅限于个人学习和非商业用途使用。
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace ScmApi.DAL    // 换成自己的命名空间
{
    public class SqlLogHelper
    {
        /// <summary>
        /// 生成日志文件
        /// 文件名格式：SqlLog+年月日+.txt
        /// </summary>
        /// <param name="Str">日志内容</param>
        public static void InsertLog(string Str)
        {
            string OldName = "SqlLog" + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + ".txt";
            string Name = "SqlLog" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\" + OldName))
            {
                System.IO.File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\" + OldName);
            }
            System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "\\" + Name, DateTime.Now + ":" + Str + "\r\n");
        }


        /// <summary>
        /// 生成日志文件
        /// 文件名格式：SqlLog+年月日+.txt
        /// </summary>
        /// <param name="Str">日志内容</param>
        /// <param name="LogType">日志所在文件夹</param>
        public static void InsertLog(string Str, string LogType)
        {
            string Year = DateTime.Now.ToString("yyyy");//年
            string Month = DateTime.Now.ToString("MM");//月

            string strFolderPath = AppDomain.CurrentDomain.BaseDirectory + "\\ScmLog";

            if (Directory.Exists(strFolderPath) == false)
            {
                Directory.CreateDirectory(strFolderPath);
            }
            strFolderPath = AppDomain.CurrentDomain.BaseDirectory + "\\ScmLog\\" + LogType;
            if (Directory.Exists(strFolderPath) == false)
            {
                Directory.CreateDirectory(strFolderPath);
            }
            string Name = "SqlLog" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "\\ScmLog\\" + LogType + "\\" + Name, DateTime.Now + ":" + Str + "\r\n");
        }
    }
}